package com.ddxz.photovoltaic.feign;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.Header;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.RequestEntity;
import org.springframework.stereotype.Component;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.util.Arrays;
import java.util.Map;

/**
 * class description
 *
 * @author 朱宗刚
 * @version 1.0.0
 * @date 2022/9/6
 */

@Component
public class DdxzHttpUtils {
    public JSONObject doPost(String url, MultiValueMap<String, String> headers, Object data) {
        try {
            RequestEntity<JSONObject> entity = data == null
                    ? new RequestEntity<>(headers, HttpMethod.POST, new URI(url))
                    : new RequestEntity<>((JSONObject) JSON.toJSON(data), headers, HttpMethod.POST, new URI(url));
            return restTemplate.postForObject(url, entity, JSONObject.class);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Resource
    private RestTemplate restTemplate;
}
